package com.centaline.androidsalesblog.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.TextUtils;
import android.view.View;

import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.adapter.viewholder.PostNewsVH;
import com.centaline.androidsalesblog.model.businissobject.CollectInfoChangeBo;
import com.centaline.androidsalesblog.model.databaseobject.PostDo;
import com.centaline.androidsalesblog.utils.DateUtil;
import com.centanet.framework.base.BaseAdapter1;

import java.util.ArrayList;
import java.util.Locale;

/**
 * 房源动态
 * Created by guilin on 16/3/30.
 */
public class PostNewsAdapter extends BaseAdapter1<PostNewsVH> {

    final ArrayList<CollectInfoChangeBo> list;

    public PostNewsAdapter(Context context, ArrayList<CollectInfoChangeBo> list) {
        super(context);
        this.list = list;
    }

    @Override
    protected int inflateByLayoutId() {
        return R.layout.item_news_post;
    }

    @Override
    protected PostNewsVH viewHolder(View view) {
        return new PostNewsVH(view);
    }

    @Override
    public void onBindViewHolder(PostNewsVH holder, int position) {
        final CollectInfoChangeBo collectInfoChangeBo = list.get(position);
        final PostDo postDo = collectInfoChangeBo.getPostDo();
        holder.img_news_point.setVisibility(collectInfoChangeBo.isRead()?View.INVISIBLE:View.VISIBLE);
        holder.atv_title.setText(postDo == null ? "获取失败" : postDo.getCnEstate());
        holder.atv_tag_fir.setText(postDo == null ? "获取失败" : String.format(Locale.CHINA, "%d室%d厅  %.0f平  %s",
                postDo.getBedroomCount(),
                postDo.getSittingRoomCount(),
                postDo.getSize(),
                postDo.getDirection() == null ? "" : postDo.getDirection()));
        holder.atv_tag_sec.setText(String.format(Locale.CHINA, "%s更新",
                DateUtil.timeRule(collectInfoChangeBo.getUpdateTime2() * 1000)));
        String changeType = collectInfoChangeBo.getChangeType();
        if ("chengjiao".equalsIgnoreCase(changeType)) {//成交
            holder.ll_price.setVisibility(View.GONE);
            holder.atv_status.setVisibility(View.VISIBLE);
            holder.atv_status.setText(R.string.sold_out);
            holder.atv_status.setBackgroundColor(Color.parseColor("#F89E9E"));
        } else if ("online".equalsIgnoreCase(changeType)) {//上下架
            holder.ll_price.setVisibility(View.GONE);
            holder.atv_status.setVisibility(View.VISIBLE);
            holder.atv_status.setText(R.string.unshelve);
            holder.atv_status.setBackgroundColor(Color.parseColor("#C2C2C2"));
        } else if ("pricechange".equalsIgnoreCase(changeType)) {//价格变更
            holder.atv_status.setVisibility(View.GONE);
            holder.ll_price.setVisibility(View.VISIBLE);
            int pricePre = 0;//变更前价格
            int price = 0;//价格变化值
            if (TextUtils.isDigitsOnly(collectInfoChangeBo.getChangeMsg1())) {
                pricePre = Integer.parseInt(collectInfoChangeBo.getChangeMsg1());
            }
            if (TextUtils.isDigitsOnly(collectInfoChangeBo.getChangeMsg2().replace("-", ""))) {
                price = Integer.parseInt(collectInfoChangeBo.getChangeMsg2());
            }
            holder.atv_price.setText(String.format(Locale.CHINA, "%d万", pricePre / 10000));
            holder.atv_price_pre.setText(pricePre > 0 ? String.format(Locale.CHINA, "%d万", (pricePre - price) /
                    10000) : "0");
            holder.atv_price_pre.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            if (price > 0) {//涨
                holder.atv_price_up.setVisibility(View.VISIBLE);
                holder.atv_price_down.setVisibility(View.GONE);
                int up = price;
                holder.atv_price_up.setText(Math.abs(up) >= 10000 ?
                        String.format(Locale.CHINA, "上涨%d万", up / 10000) :
                        String.format(Locale.CHINA, "上涨%d", up));
                holder.atv_price.setTextColor(Color.parseColor("#F13E06"));
            } else {
                holder.atv_price_up.setVisibility(View.GONE);
                holder.atv_price_down.setVisibility(View.VISIBLE);
                int down = price * -1;
                holder.atv_price_down.setText(Math.abs(down) >= 10000 ?
                        String.format(Locale.CHINA, "下跌%d万", down / 10000) :
                        String.format(Locale.CHINA, "下跌%d", down));
                holder.atv_price.setTextColor(Color.parseColor("#3A9A5E"));
            }
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
}
